#include "AERepeatKey.hpp"

BEGIN_ARETE_NAMESPACE(input)

	AERepeatKey::AERepeatKey(float _fRepeatDelay, float _fInitialDelay) :
	m_key(OIS::KC_UNASSIGNED),
	m_fRepeatDelay(_fRepeatDelay),
	m_fInitialDelay(_fInitialDelay)
{
}

void AERepeatKey::begin( const OIS::KeyEvent& _evt)
{
	m_key = _evt.key;
	m_char = _evt.text;

	m_fElapsed = 0;
	m_fDelay = m_fInitialDelay;
}

void AERepeatKey::end( const OIS::KeyEvent& _evt)
{
	if(m_key != _evt.key) return;

	m_key = OIS::KC_UNASSIGNED;
}

void AERepeatKey::repeat( float _delta)
{
	if(m_key == OIS::KC_UNASSIGNED) return;

	m_fElapsed += _delta;
	if(m_fElapsed < m_fDelay) return;

	m_fElapsed -= m_fDelay;
	m_fDelay = m_fRepeatDelay;

	do {
		repeatKey(m_key,m_char);

		m_fElapsed -= m_fRepeatDelay;
	} while(m_fElapsed >= m_fRepeatDelay);

	m_fElapsed = 0;
}

END_ARETE_NAMESPACE